﻿using System.Collections.Generic;
using Yuebon.CMS.Dtos;
using Yuebon.CMS.IServices;
using Yuebon.CMS.Models;
using Yuebon.Commons;
using Yuebon.Commons.IoC;
using Yuebon.Commons.Models;

namespace Yuebon.CMS.Application
{
    public class BannerApp
    {

        IBannerService serviceBanner = IoCContainer.Resolve<IBannerService>();
        IAdvertService serviceAdvert = IoCContainer.Resolve<IAdvertService>();

        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="condition">查询的条件</param>
        /// <param name="info">分页实体</param>
        /// <param name="fieldToSort">排序字段</param>
        /// <param name="desc">是否降序</param>
        /// <returns>指定对象的集合</returns>
        public List<BannerDto> FindWithPager(string where, PagerInfo pagerInfo, string fieldToSort, bool desc)
        {
            List<Banner> list= serviceBanner.FindWithPager(where, pagerInfo, fieldToSort, desc);
            List<BannerDto> resultList = new List<BannerDto>();
            foreach(Banner item in list)
            {
                BannerDto bannerDto = new BannerDto();
                bannerDto.Id = item.Id;
                bannerDto.Title = item.Title;
                bannerDto.AdId = item.AdId;
                bannerDto.ADTitle = serviceAdvert.Get(item.AdId).Title;
                bannerDto.SubTitle = item.SubTitle;
                bannerDto.Zhaiyao = item.Zhaiyao;
                bannerDto.Description = item.Description;
                bannerDto.StartTime = item.StartTime;
                bannerDto.EndTime = item.EndTime;
                bannerDto.FilePath = item.FilePath;
                bannerDto.LinkUrl = item.LinkUrl;
                bannerDto.SortCode = item.SortCode;
                bannerDto.EnabledMark = item.EnabledMark;
                bannerDto.DeleteMark = item.DeleteMark;
                bannerDto.CreatorTime = item.CreatorTime;
                bannerDto.CreatorUserId = item.CreatorUserId;
                bannerDto.CompanyId = item.CompanyId;
                bannerDto.DeptId = item.DeptId;
                bannerDto.LastModifyTime = item.LastModifyTime;
                bannerDto.LastModifyUserId = item.LastModifyUserId;
                bannerDto.DeleteTime = item.DeleteTime;
                bannerDto.DeleteUserId = item.DeleteUserId;
                

                resultList.Add(bannerDto);
            }
            return resultList;
        }

        public List<BannerDto> GetListWhere(string where)
        {
            IEnumerable<Banner> list = serviceBanner.GetListWhere(where);
            List<BannerDto> resultList=new List<BannerDto>();
            foreach (Banner item in list)
            {
                BannerDto bannerDto = new BannerDto();
                bannerDto.Id = item.Id;
                bannerDto.Title = item.Title;
                bannerDto.AdId = item.AdId;
                bannerDto.ADTitle = serviceAdvert.Get(item.AdId).Title;
                bannerDto.SubTitle = item.SubTitle;
                bannerDto.Zhaiyao = item.Zhaiyao;
                bannerDto.Description = item.Description;
                bannerDto.StartTime = item.StartTime;
                bannerDto.EndTime = item.EndTime;
                bannerDto.FilePath =item.FilePath;
                bannerDto.LinkUrl = item.LinkUrl;
                bannerDto.SortCode = item.SortCode;
                bannerDto.EnabledMark = item.EnabledMark;
                bannerDto.DeleteMark = item.DeleteMark;
                bannerDto.CreatorTime = item.CreatorTime;
                bannerDto.CreatorUserId = item.CreatorUserId;
                bannerDto.CompanyId = item.CompanyId;
                bannerDto.DeptId = item.DeptId;
                bannerDto.LastModifyTime = item.LastModifyTime;
                bannerDto.LastModifyUserId = item.LastModifyUserId;
                bannerDto.DeleteTime = item.DeleteTime;
                bannerDto.DeleteUserId = item.DeleteUserId;


                resultList.Add(bannerDto);
            }
            return resultList;
        }

        public BannerDto GetWhere(string where)
        {
            Banner item = serviceBanner.GetWhere(where);
            BannerDto bannerDto = new BannerDto();
            if (item != null)
            {
                bannerDto.Id = item.Id;
                bannerDto.Title = item.Title;
                bannerDto.AdId = item.AdId;
                bannerDto.ADTitle = serviceAdvert.Get(item.AdId).Title;
                bannerDto.SubTitle = item.SubTitle;
                bannerDto.Zhaiyao = item.Zhaiyao;
                bannerDto.Description = item.Description;
                bannerDto.StartTime = item.StartTime;
                bannerDto.EndTime = item.EndTime;
                bannerDto.FilePath = Configs.GetConfigurationValue("AppSetting", "FileUrl") + item.FilePath;
                bannerDto.LinkUrl = item.LinkUrl;
                bannerDto.SortCode = item.SortCode;
                bannerDto.EnabledMark = item.EnabledMark;
                bannerDto.DeleteMark = item.DeleteMark;
                bannerDto.CreatorTime = item.CreatorTime;
                bannerDto.CreatorUserId = item.CreatorUserId;
                bannerDto.CompanyId = item.CompanyId;
                bannerDto.DeptId = item.DeptId;
                bannerDto.LastModifyTime = item.LastModifyTime;
                bannerDto.LastModifyUserId = item.LastModifyUserId;
                bannerDto.DeleteTime = item.DeleteTime;
                bannerDto.DeleteUserId = item.DeleteUserId;
            }
            return bannerDto;
        }
    }
}
